<?php
  include 'header.php';
  if ($_SERVER['REQUEST_METHOD'] != 'GET') {
      echo 'This file cannot be called directly.';
  } else {
      if ($_SESSION['user_level'] < 1) {
          //the user is not an admin
          header('location: signin.php');
          echo '<br /><a href="index.php?id=' . mysql_real_escape_string($_GET['p']) . '">Back to index</a>.';
      } else {
          $sql = mysql_query("SELECT * FROM " . $table_prefix . "posts WHERE post_topic = " . mysql_real_escape_string($_GET['t']) . "");
          if (!$sql) {
              echo "Error selecting database!";
          } else {
              //DELETE CACHE  
              if ($forum_cache = "1") {
                  $delete_cache = mysql_query("SELECT topic_cat FROM " . $table_prefix . "topics WHERE topic_id = " . mysql_real_escape_string($_GET['t']) . "");
                  $cache = mysql_fetch_assoc($delete_cache);
                  if (file_exists("./cache/forum" . $cache['topic_cat'] . ".php")) {
                      unlink("./cache/forum" . $cache['topic_cat'] . ".php");
                  }
              }
              // END DELETE CACHE 
              
              $cat_update3 = mysql_query("SELECT topic_cat FROM topics WHERE topic_id = " . mysql_real_escape_string($_GET['t']) . " LIMIT 1");
              
              $result3 = mysql_query("DELETE FROM " . $table_prefix . "topics WHERE topic_id = " . mysql_real_escape_string($_GET['t']) . "");
              
              $result4 = mysql_query("DELETE FROM " . $table_prefix . "topics_watch WHERE topic_id = " . mysql_real_escape_string($_GET['t']) . "");
              
              $tcat = mysql_fetch_assoc($cat_update3);
              $cat_update33 = mysql_query("SELECT * FROM topics WHERE topic_cat = " . $tcat['topic_cat'] . " ORDER BY topic_last_post_date DESC LIMIT 1");
              
              if (mysql_num_rows($cat_update33) >= 1) {
                  $row_cat = mysql_fetch_assoc($cat_update33);
                  $cat_last_date = $row_cat['topic_last_post_date'];
                  $cat_last_postid = $row_cat['topic_id'];
                  $cat_last_poster = $row_cat['topic_last_poster'];
                  $cat_last_posterid = $row_cat['topic_last_poster_id'];
                  $cat_last_tsubject = $row_cat['topic_subject'];
              }
              
              if (mysql_num_rows($cat_update33) <= 0) {
                  $cat_last_date = "";
                  $cat_last_postid = 0;
                  $cat_last_poster = "";
                  $cat_last_posterid = 0;
                  $cat_last_tsubject = "";
              }
              
              
              $cat_update4 = mysql_query("UPDATE " . $table_prefix . "categories 
SET 
cat_last_post_time='" . $cat_last_date . "',
cat_last_post_id='" . $cat_last_postid . "',
cat_last_poster_name='" . $cat_last_poster . "',
cat_last_poster_id=" . $cat_last_posterid . ",
cat_last_post_subject = '" . $cat_last_tsubject . "' 
WHERE cat_id =" . $tcat['topic_cat'] . "") or die(mysql_error());
              
              echo "<b>TOPIC " . mysql_real_escape_string($_GET['t']) . " deleted!</b><br />";
              if (mysql_num_rows($sql) > 0) {
                  while ($row = mysql_fetch_assoc($sql)) {
                      echo "PostID: " . $row['post_id'] . " | ";
                      
                      $sql2 = mysql_query("SELECT * FROM " . $table_prefix . "users WHERE user_id = " . $row['post_by'] . "");
                      while ($row2 = mysql_fetch_assoc($sql2)) {
                          echo "User: " . $row2['user_name'] . " | posts: " . $row2['user_posts'] . "<br />";
                          
                          $result = mysql_query("DELETE FROM " . $table_prefix . "posts WHERE post_id = " . $row['post_id'] . "");
                          echo "<b>Post " . $row['post_id'] . " deleted!</b><br />";
                          $result2 = mysql_query("UPDATE " . $table_prefix . "users SET user_posts = user_posts-1 WHERE user_id = " . $row2['user_id'] . "");
                      }
                  }
              }
          }
      }
  }
  if ($pageTitle = "") {
      $pageTitle = $l_forumname;
  }
  // Get all the page's HTML into a string
  $pageContents = ob_get_contents();
  // Wipe the buffer 
  ob_end_clean();
  echo str_replace('<!--TITLE-->', $pageTitle, $pageContents);
?>